package org.hxzon.workeasy.pages.job;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;
import org.hxzon.workeasy.model.EasyJob;
import org.hxzon.workeasy.pages.base.MyBasePage;
import org.hxzon.workeasy.service.EasyJobService;

public class ListJob extends MyBasePage {
    @Inject
    private EasyJobService jobSer;

    @Property
    private List<EasyJob> list;
    @Property
    private String type = "today";
    @Property
    private SplitPageInfo page = new SplitPageInfo();

    Object initialize(String type) {
        init(type, 50, 1);
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String type, long size, long first) {
        this.type = type;
        page.setCurrentPage(first);
        page.setPageSize(size);
        return null;
    }

    @SetupRender
    Object display() {
        String username = getVisitor().getUsername();
        if ("all".equals(type)) {
            page.setRowCount(jobSer.countAllJob(username));
            list = jobSer.findAllJob(username, page.getCurrentRow(), page.getPageSize());
        } else if ("current".equals(type)) {
            page.setRowCount(jobSer.countThisHourJob(username));
            list = jobSer.findThisHourJob(username, page.getCurrentRow(), page.getPageSize());
        } else {
            page.setRowCount(jobSer.countTodayJob(username));
            list = jobSer.findTodayJob(username, page.getCurrentRow(), page.getPageSize());
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { type, page.getPageSize(), page.getCurrentPage() };
    }

}
